﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GolfLeague.EditModels;
using GolfLeague.Models;
using GolfLeague.ViewModels;

namespace GolfLeague.Controllers
{
    public class PlayerController : Controller
    {
        PlayerInterface playerInterface = new PlayerInterface();
        HandicapInterface handicapInterface = new HandicapInterface();

        //
        // GET: /Player/

        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            PlayerEditModel player = new PlayerEditModel
            {
                Players = playerInterface.GetAllPlayers(),
                NewPlayer = new Player(),
                NewHandicap = new PlayerHandicap()
            };

            return View(player);
            //return View(new PlayerEditModel
            //{
            //    Players = playerInterface.GetAllPlayers(),
            //    NewPlayer = new Player(),
            //    NewHandicap = new PlayerHandicap()
            //});
        }

        //
        // POST: /Player/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Index(PlayerEditModel playerEditModel)
        {
            if (ModelState.IsValid)
            {
                playerInterface.AddPlayer(playerEditModel.NewPlayer);
                playerInterface.Save();
                playerEditModel.NewHandicap.PlayerID = playerEditModel.NewPlayer.PlayerID;
                handicapInterface.AddHandicap(playerEditModel.NewHandicap);
                handicapInterface.Save();

                return RedirectToAction("Index");
            }
            else
            {
                return View(new PlayerEditModel
                {
                    Players = playerInterface.GetAllPlayers(),
                    NewPlayer = playerEditModel.NewPlayer
                });
            }
        }

        //
        // GET: /Player/PlayerEdit/

        [Authorize(Roles = "Administrator")]
        public ActionResult PlayerEdit(int id)
        {
            //return View(playerInterface.GetPlayer(id));
            return View("Index", new PlayerEditModel
            {
                Players = playerInterface.GetAllPlayers(),
                NewPlayer = playerInterface.GetPlayer(id),
                NewHandicap = playerInterface.GetPlayersCurrentHandicap(id)
            });
        }

        //
        // POST: /Player/PlayerEdit/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult PlayerEdit(int id, PlayerEditModel playerEditModel)
        {
            if (ModelState.IsValid)
            {
                var updatePlayer = playerInterface.GetPlayer(id);
                updatePlayer.FirstName = playerEditModel.NewPlayer.FirstName;
                updatePlayer.LastName = playerEditModel.NewPlayer.LastName;
                updatePlayer.PhoneNumber = playerEditModel.NewPlayer.PhoneNumber;
                updatePlayer.Email = playerEditModel.NewPlayer.Email;
                updatePlayer.Sub = playerEditModel.NewPlayer.Sub;

                if (TryUpdateModel(updatePlayer))
                {
                    playerInterface.Save();
                    return RedirectToAction("Index");
                }
                else
                {
                    return View("Index", new PlayerEditModel
                    {
                        Players = playerInterface.GetAllPlayers(),
                        NewPlayer = playerEditModel.NewPlayer
                    });
                }
            }
            else
            {
                return View("Index", new PlayerEditModel
                {
                    Players = playerInterface.GetAllPlayers(),
                    NewPlayer = playerEditModel.NewPlayer
                });
            }
        }

        //
        // POST: /Player/PlayerDelete/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult PlayerDelete(int playerID)
        {
            Player player = playerInterface.GetPlayer(playerID);
            player.EndedOn = DateTime.Now;

            //playerInterface.DeletePlayer(player);
            UpdateModel(player);
            playerInterface.Save();

            return RedirectToAction("Index");
        }

    }
}
